<?php
function getnews($cate_id, $row = 7) {
	$map [] = [ 'n.cate_id','=',$cate_id ];
	$map [] = [ 'n.status','=',1 ];
	return model ( 'News' )->getTop ( $map, 'n.id desc', $row );
}
function getphoto($cate_id, $row = 7) {
	$map [] = [ 'n.cate_id','=',$cate_id ];
	$map [] = [ 'n.status','=',1 ];
	return model ( 'Photo' )->getTop ( $map, 'n.id desc', $row );
}

function root_path($path = '')
{
    return app()->getRootPath() . ($path ? $path . DIRECTORY_SEPARATOR : $path);
}

function logger($name, $data, $backtrace = false){
    $name = valid_path($name);
    $path = root_path(). "public/logs/";

    if(!is_dir($path) && !mkdir($path, 0755, true)){
        return false;
    }


//    if(stripos(real_path($path), root_path()) !== 0){
//        return false;
//    }
//    dd($path);
    if(
        !($fp = fopen($path ."/". $name.".log", $backtrace === null ? 'w' : 'ab')) ||
        !flock($fp, LOCK_EX)
    ){
        return false;
    }

    if(is_array($data) || is_object($data)){
        $data = var_export($data, true);
    }
    if($backtrace){
        $d = debug_backtrace();
        foreach($d as $v){
            if(empty($v['file'])) continue;

            $data .= "\n$v[file]: $v[line]";
        }
    }
    $ret = fputs(
        $fp,
        date('#Y-m-d H:i:s') ."\n". $data ."\n\n"
    );
    flock($fp, LOCK_UN);
    fclose($fp);
    return $ret;
}

function valid_path($path){
    return str_replace(['\\', ASCII0], ['/', ''], $path);
}

function real_path($path){
    $path = valid_path(realpath($path));
    return is_dir($path) && substr($path, -1) != '/' ? $path .'/' : $path;
}

function dd($dump){
    echo "<pre>";
    print_r($dump);
    echo "<pre>";
    die();
}